package com.lvyq.javaprint.controller;

import com.lvyq.javaprint.model.ProxySettingModel;
import com.lvyq.javaprint.util.SettingUtils;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import org.dom4j.DocumentException;

import java.io.IOException;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2025-01-09 9:54
 */
public class FXProxySettingController {
    private final ProxySettingModel proxySettingModel = new ProxySettingModel();

    //类型下拉数据
    static ComboBox<String> comboBox = new ComboBox<>();
    //ip地址
    static TextField domainField = new TextField();
    //端口
   static TextField portField = new TextField();
   //服务端口
   static TextField serverPortField = new TextField();

    public FXProxySettingController(ComboBox<String> comboBox, TextField domainField, TextField portField,TextField serverPortField) {
        this.comboBox = comboBox;
        this.domainField = domainField;
        this.portField = portField;
        this.serverPortField =serverPortField;
        //双向绑定组件数据
        comboBox.valueProperty().bindBidirectional(proxySettingModel.typeProperty());
        domainField.textProperty().bindBidirectional(proxySettingModel.domainProperty());
        portField.textProperty().bindBidirectional(proxySettingModel.portProperty());
        serverPortField.textProperty().bindBidirectional(proxySettingModel.serverPortProperty());
    }



    /**
     * @description 加载数据
     * @author lvyq
     * @throws
     * @time 2025-01-09 10:58
     */
    public  void loadData(){
        //加载数据
        proxySettingModel.loadData();
    }


    /**
     * @description 更新设置
     * @author lvyq
     * @throws
     * @time 2025-01-09 16:13
     */
    public void updateSetting(){
        try {
            SettingUtils.writeProxySetting(proxySettingModel);
            SettingUtils.writeServerPort(proxySettingModel.getServerPort());
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
